package com.dreamwhale.machinery.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.config.UploadFile;
import com.dreamwhale.machinery.dao.*;
import com.dreamwhale.machinery.entity.*;
import com.dreamwhale.machinery.goods.entity.*;
import com.dreamwhale.machinery.goods.mapper.*;
import com.dreamwhale.machinery.goods.service.*;
import com.dreamwhale.machinery.logistics.entity.TransportCompanyInfo;
import com.dreamwhale.machinery.logistics.entity.TransportInfo;
import com.dreamwhale.machinery.logistics.service.TransportCompanyInfoService;
import com.dreamwhale.machinery.logistics.service.TransportInfoService;
import com.dreamwhale.machinery.service.*;
import com.dreamwhale.machinery.user.entity.UserShop;
import com.dreamwhale.machinery.user.entity.YxUser;
import com.dreamwhale.machinery.user.entity.YxUserBill;
import com.dreamwhale.machinery.user.mapper.UserShopMapper;
import com.dreamwhale.machinery.user.mapper.YxUserBillMapper;
import com.dreamwhale.machinery.user.mapper.YxUserMapper;
import com.dreamwhale.machinery.user.service.YxUserBillService;
import com.dreamwhale.machinery.user.service.YxUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.lang.management.BufferPoolMXBean;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 新机展 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@RestController
@RequestMapping("/user")
public class MachineShowController {

    @Resource
    private YxUserMapper yxUserMapper;
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private YxUserService yxUserService;
    @Resource
    private YxUserWithdrawMapper yxUserWithdrawMapper;
    @Resource
    private YxUserBillService yxUserBillService;
    @Resource
    private YxUserBillMapper yxUserBillMapper;
    @Resource
    private BankMapper bankMapper;
    @Resource
    private MachinistService machinistService;
    @Resource
    private ProductModelMapper productModelMapper;
    @Resource
    private YxStoreOrderService yxStoreOrderService;
    @Resource
    private YxStoreProductService yxStoreProductService;
    @Resource
    private UserShopMapper userShopMapper;
    @Resource
    private ProductBrandMapper productBrandMapper;
    @Resource
    private MachineBuyService machineBuyService;
    @Resource
    private ProjectSeekService projectSeekService;
    @Resource
    private DriverInfoService driverInfoService;
    @Resource
    private DriverWorkInfoService driverWorkInfoService;
    @Resource
    private MachineSellService machineSellService;
    @Resource
    private TransportCompanyInfoService transportCompanyInfoService;
    @Resource
    private TransportInfoService transportInfoService;
    @Resource
    private VipArticleService vipArticleService;
    @Resource
    private VipArticleCollectService vipArticleCollectService;
    @Resource
    private VipArticleCollectMapper vipArticleCollectMapper;
    @Resource
    private YxUserFeedbackMapper yxUserFeedbackMapper;
    @Resource
    private InfoCollectService infoCollectService;
    @Resource
    private LeaseService leaseService;
    @Resource
    private VipUserInfoMapper vipUserInfoMapper;
    @Resource
    private UploadFile uploadFile;
    @Resource
    private YxStoreCategoryMapper yxStoreCategoryMapper;
    @Resource
    private CarTypesMapper carTypesMapper;
    @Resource
    private VipInfoMapper vipInfoMapper;
    @Resource
    private YxStoreCartMapper yxStoreCartMapper;
    @Resource
    private DictDetailMapper dictDetailMapper;
    @Resource
    private YxStoreProductAttrValueMapper yxStoreProductAttrValueMapper;
    /**
     * 根据用户id查询用户信息
     *
     * @param userId
     * @return
     */
    @RequestMapping("/selectYxUserByUserId")
    public DataResult selectYxUserByUserId(Integer userId) {

        return DataResult.ok(yxUserService.selectYxUserByUserId(userId));
    }


    /**
     * 修改我的用户信息
     *
     * @param yxUser
     * @return
     */
    @RequestMapping("/updateYxUserByUserId")
    public DataResult updateYxUserByUserId(YxUser yxUser) {
        int count = yxUserMapper.updateById(yxUser);
        if (count > 0) {
            return DataResult.ok("1");
        }
        return DataResult.build(300, "删除失败");
    }


    /**
     * 查询公告
     *
     * @param
     * @return
     */
    @RequestMapping("/selectNews")
    public DataResult selectNews() {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("news_del", 0);

        return DataResult.ok(newsMapper.selectList(queryWrapper));
    }

    /**
     * 根据id查询公告
     *
     * @param
     * @return
     */
    @RequestMapping("/selectNewsByNewsId")
    public DataResult selectNewsByNewsId(Integer newsId) {

        return DataResult.ok(newsMapper.selectById(newsId));
    }


    /**
     * 提现
     *
     * @param
     * @return
     */
    @RequestMapping("/insertYxUserWithdraws")
    @Transactional
    public DataResult insertYxUserWithdraws(YxUserWithdraw yxUserWithdraw) {
        //查询余额

        YxUser yxUser = yxUserMapper.selectById(yxUserWithdraw.getUserId());
        if (yxUser.getNowMoney().compareTo(yxUserWithdraw.getWithdrawPrice()) >= 0) {
            //插入提现申请
            yxUserWithdraw.setWithdrawCreateTime(LocalDateTime.now());
            yxUserWithdrawMapper.insert(yxUserWithdraw);
            //修改钱包
            yxUser.setNowMoney(yxUser.getNowMoney().subtract(yxUserWithdraw.getWithdrawPrice()));
            yxUserMapper.updateById(yxUser);
            //插入收支记录
            YxUserBill yxUserBill = new YxUserBill();
            yxUserBill.setUid(yxUserWithdraw.getUserId());
            yxUserBill.setPm(0);//支出
            yxUserBill.setCategory("提现支出");
            yxUserBill.setBillNumber(yxUserWithdraw.getWithdrawPrice());
            yxUserBill.setBalance(yxUser.getNowMoney());
            yxUserBillService.insertYxUserBills(yxUserBill);
            return DataResult.ok("1");
        }
        return DataResult.build(300, "余额不足");
    }


    /**
     * 查询我的收支记录
     *
     * @param userId
     * @return
     */
    @RequestMapping("/selectYxUserBillsByUserId")
    public DataResult selectYxUserBillsByUserId(Integer page, Integer limit, Integer userId) {

        PageHelper.startPage(page, limit, true);
        QueryWrapper<YxUserBill> queryWrapper = new QueryWrapper<YxUserBill>();
        queryWrapper.eq("uid", userId).orderByDesc("add_time");
        List<YxUserBill> list = yxUserBillMapper.selectList(queryWrapper);
        PageInfo<YxUserBill> pages = new PageInfo<>(list);
        return DataResult.ok(pages);
    }


    /**
     * 根据商户id查询贷款购机
     *
     * @param merId
     * @return
     */
    @RequestMapping("/selectBankByMerId")
    public DataResult selectBankByMerId(Integer merId) {
        QueryWrapper<Bank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mer_id", merId).eq("del_flag", 0);
        return DataResult.ok(bankMapper.selectList(queryWrapper));
    }


    /**
     * 机修师入驻
     *
     * @param machinist
     * @return
     */
    @RequestMapping("/insertMachinists")
    public DataResult insertMachinists(Machinist machinist) {
        int count = machinistService.insertMachinists(machinist);
        if (count > 0) {
            return DataResult.ok("1");
        }
        return DataResult.build(300, "删除失败");
    }


    /**
     * 查询机修师
     *
     * @param provinceCode
     * @param cityCode
     * @param classifyId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selectMachinistsByCity")
    public DataResult selectMachinistsByCity(@Param("provinceCode") String provinceCode, @Param("cityCode") String cityCode,
                                             @Param("classifyId") Integer classifyId,
                                             Integer page, Integer limit) {

        PageHelper.startPage(page, limit, true);
        List<Machinist> list = machinistService.selectMachinistsByCity(provinceCode, cityCode, classifyId);

        for (Machinist machinist : list) {
            //查询用户昵称，头像
            QueryWrapper<YxUser> listy = new QueryWrapper<>();
            listy.eq("uid", machinist.getUserId());
            List<YxUser> yxUsers = yxUserMapper.selectList(listy);
            machinist.setNickname(yxUsers.get(0).getNickname());
            machinist.setAvatar(yxUsers.get(0).getAvatar());
            //查询分类
            QueryWrapper<YxStoreCategory> listc = new QueryWrapper<>();
            listc.eq("id", machinist.getClassifyId());
            YxStoreCategory classify = yxStoreCategoryMapper.selectOne(listc);
            if (machinist.getClassifyId() != null) {
                machinist.setClassifyName(classify.getCateName());
            }

        }
        PageInfo<Machinist> pages = new PageInfo<>(list);
        return DataResult.ok(pages);
    }


    /**
     * 根据id查询机修师详情
     *
     * @param machinistId
     * @return
     */
    @RequestMapping("/selectMachinistsByMaId")
    public DataResult selectMachinistsByMaId(Integer machinistId) {


        Machinist machinist = machinistService.selectMachinistsByMaId(machinistId);

        //查询用户昵称，头像
        QueryWrapper<YxUser> listy = new QueryWrapper<>();
        listy.eq("uid", machinist.getUserId());
        List<YxUser> yxUsers = yxUserMapper.selectList(listy);
        machinist.setNickname(yxUsers.get(0).getNickname());
        machinist.setAvatar(yxUsers.get(0).getAvatar());
        //查询分类
        QueryWrapper<YxStoreCategory> listc = new QueryWrapper<>();
        listc.eq("id", machinist.getClassifyId());
        YxStoreCategory classify = yxStoreCategoryMapper.selectOne(listc);
        if (machinist.getClassifyId() != null) {
            machinist.setClassifyName(classify.getCateName());
        }
        //查询型号
        QueryWrapper<ProductModel> listp = new QueryWrapper<>();
        listy.eq("id", machinist.getModelId());
        ProductModel productBrand = productModelMapper.selectOne(listp);
        if (machinist.getModelId() != null) {
            machinist.setModelName(productBrand.getModelName());
        }

        return DataResult.ok(machinist);
    }

    /**
     * 删除
     *
     * @param yxStoreOrder
     * @return
     */
    @RequestMapping("/deleteYxStoreOrder")
    public DataResult deleteYxStoreOrder(YxStoreOrder yxStoreOrder) {
        yxStoreOrder.setIsDel(true);//删除
        int count = yxStoreOrderService.updateYxStoreOrders(yxStoreOrder);
        if (count > 0) {
            return DataResult.ok("1");
        }
        return DataResult.build(300, "删除失败");
    }


    /**
     * 确认收货
     *
     * @param yxStoreOrder
     * @return
     */
    @RequestMapping("/updateYxStoreOrderTake")
    public DataResult updateYxStoreOrderTake(YxStoreOrder yxStoreOrder) {
        yxStoreOrder.setStatus(2);//收货
        int count = yxStoreOrderService.updateYxStoreOrders(yxStoreOrder);
        if (count > 0) {
            //查询订单金额
            YxStoreOrder yxStoreOrder1= yxStoreOrderService.getById(yxStoreOrder.getId());
            //给商户钱包加钱
            String[] a = yxStoreOrder1.getCartId().split(",");
            //查询订单的商品是哪个商户的
            for (String string : a) {

                //购物车
                YxStoreCart yxStoreCart = yxStoreCartMapper.selectById(string);
                //商品
                YxStoreProduct storeProduct = yxStoreProductService.getById(yxStoreCart.getProductId());
                //属性价格
                YxStoreProductAttrValue yxStoreProductAttrValue = yxStoreProductAttrValueMapper.
                        selectOne(new QueryWrapper<YxStoreProductAttrValue>().eq("product_id", yxStoreCart.getProductId())
                                .eq("suk", yxStoreCart.getProductAttrUnique()));
                //商户
                UserShop userShop=userShopMapper.selectById(storeProduct.getMerId());
                //用户
                YxUser yxUser=yxUserMapper.selectById(userShop.getUid());
                //当前余额+商品价格
                yxUser.setNowMoney(yxUser.getNowMoney().add(yxStoreProductAttrValue.getPrice()));
                yxUserMapper.updateById(yxUser);

                //插入用户收支记录
                YxUserBill yxUserBill=new YxUserBill();
                yxUserBill.setUid(yxUser.getUid());
                yxUserBill.setPm(1);//获得
                yxUserBill.setTitle("商品卖出");
                yxUserBill.setCategory("now_money");
                yxUserBill.setBillType("pay_product");
                yxUserBill.setBillNumber(yxStoreProductAttrValue.getPrice());
                yxUserBill.setBalance(yxUser.getNowMoney());
                yxUserBill.setMark("商品卖出用户确认收货");
                yxUserBill.setAddTime(LocalDateTime.now());
                yxUserBillMapper.insert(yxUserBill);

                //查询订单是否有推荐人
                if(yxStoreCart.getShareId()!=0){
                    //分享人信息
                    YxUser yxUser1=yxUserMapper.selectById(yxStoreCart.getShareId());
                    //查询佣金百分比
                    QueryWrapper<DictDetail> dictDetailQueryWrapper = new QueryWrapper<>();
                    dictDetailQueryWrapper.lambda().eq(DictDetail::getLabel,"rebate");
                    DictDetail dictDetail=dictDetailMapper.selectOne(dictDetailQueryWrapper);
                    //算钱+分钱
                    BigDecimal moneys=new BigDecimal(dictDetail.getValue()).divide(BigDecimal.valueOf(100));
                    BigDecimal money= yxStoreOrder1.getTotalPrice().multiply(moneys);
                    yxUser1.setNowMoney(yxUser1.getNowMoney().add(money));
                    yxUserMapper.updateById(yxUser1);
                    //插入用户收支记录
                    YxUserBill yxUserBill1=new YxUserBill();
                    yxUserBill1.setUid(yxUser1.getUid());
                    yxUserBill1.setPm(1);//获得
                    yxUserBill1.setTitle("推广商品返佣");
                    yxUserBill1.setCategory("now_money");
                    yxUserBill1.setBillType("brokerage");
                    yxUserBill1.setBillNumber(money);
                    yxUserBill1.setBalance(yxUser1.getNowMoney());
                    yxUserBill1.setMark("分享用户购买商品返佣");
                    yxUserBill1.setAddTime(LocalDateTime.now());
                    yxUserBillMapper.insert(yxUserBill1);
                }
            }

            return DataResult.ok("1");

        }
        return DataResult.build(300, "确认失败");
    }

    /**
     * 申请退款
     *
     * @param yxStoreOrder
     * @return
     */
    @RequestMapping("/updateYxStoreOrderRefund")
    public DataResult updateYxStoreOrderRefund(YxStoreOrder yxStoreOrder) {
        yxStoreOrder.setStatus(-1);//退款
        yxStoreOrder.setRefundStatus(1);//申请中
        int count = yxStoreOrderService.updateYxStoreOrders(yxStoreOrder);
        if (count > 0) {
            return DataResult.ok("1");
        }
        return DataResult.build(300, "确认失败");
    }

    /**
     * 查询余额
     *
     * @param userId
     * @return
     */
    @RequestMapping("selectUserMoney")
    public DataResult selectUserMoney(Integer userId) {

        return DataResult.ok(yxUserMapper.selectById(userId));
    }

    /**
     * 查询新机展
     *
     * @param brandId
     * @param cateId
     * @param priceId
     * @param storeName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selectYxStoreProductNews")
    public DataResult selectYxStoreProductNews(Integer brandId, Integer cateId,
                                               Integer priceId, String storeName, Integer page, Integer limit) {

        PageHelper.startPage(page, limit, true);
        List<YxStoreProduct> list = yxStoreProductService.selectYxStoreProductNews(brandId, cateId, priceId, storeName);
        //查询商户昵称
        for (YxStoreProduct yxStoreProduct : list) {
            QueryWrapper<UserShop> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", yxStoreProduct.getMerId());
            List<UserShop> lists = userShopMapper.selectList(queryWrapper);
            if (yxStoreProduct.getMerId() != 0 && !CollectionUtils.isEmpty(lists)) {
                yxStoreProduct.setMerchantName(lists.get(0).getMerchantName());
            }
            //查询品牌
            QueryWrapper<ProductBrand> listb = new QueryWrapper<>();
            listb.eq("id", yxStoreProduct.getBrandId());
            ProductBrand brand = productBrandMapper.selectOne(listb);
            if (brand != null) {
                yxStoreProduct.setBrandName(yxStoreProduct.getBrandId() == null ? null : brand.getName());
            }
        }

        PageInfo<YxStoreProduct> pages = new PageInfo<>(list);
        return DataResult.ok(pages);
    }


    /**
     * 首页查询信息
     *
     * @param
     * @param page
     * @param limit 1   司机招聘  2   司机找工作  3   找机械   4   二手机械出售     5   找工程 6   物流信息 7   托运信息
     * @return
     */
    @RequestMapping("/selectHomePageMessage")
    public DataResult selectHomePageMessage(Integer page, Integer limit, @Param("salaryMin") Integer salaryMin, @Param("salaryMax") Integer salaryMax,
                                            @Param("years") Integer years, @Param("province") String province, @Param("city") String city,
                                            @Param("types") Integer types, @Param("type") Integer type, @Param("modelId") Integer modelId,
                                            @Param("brandId") Integer brandId, String title, String driveTypeId,
                                            Integer accessType, Integer carTypeId) {

        PageHelper.startPage(page, limit, true);
        if (accessType == 1) {
            //找司机
            List<DriverInfo> driverInfo = driverInfoService.selectDriverInfoHome(salaryMin, salaryMax, years, province, city, type, types, title, driveTypeId);
            PageInfo<DriverInfo> pages = new PageInfo<>(driverInfo);
            return DataResult.ok(pages);
        } else if (accessType == 2) {
            //找工作
            List<DriverWorkInfo> driverWorkInfo = driverWorkInfoService.selectDriverWorkInfoHome(salaryMin, salaryMax, years, province, city, type, types, title, driveTypeId);
            PageInfo<DriverWorkInfo> pages = new PageInfo<>(driverWorkInfo);
            return DataResult.ok(pages);
        } else if (accessType == 3) {
            //找机械
            List<MachineBuy> machineBuy = machineBuyService.selectMachineBuyHome(type, types, brandId, modelId, title);
            for (MachineBuy machineBuy1 : machineBuy) {
                QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", machineBuy1.getUserId());
                YxUser listy = yxUserMapper.selectOne(queryWrapper);
                machineBuy1.setNickname(listy.getNickname());
                machineBuy1.setAvatar(listy.getAvatar());
            }
            PageInfo<MachineBuy> pages = new PageInfo<>(machineBuy);
            return DataResult.ok(pages);

        } else if (accessType == 4) {
            //卖机械
            List<MachineSell> machineSell = machineSellService.selectMachineSellHome(type, types, brandId, modelId, title);
            for (MachineSell machineBuy1 : machineSell) {
                QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", machineBuy1.getUserId());
                YxUser listy = yxUserMapper.selectOne(queryWrapper);
                machineBuy1.setNickname(listy.getNickname());
                machineBuy1.setAvatar(listy.getAvatar());
            }
            PageInfo<MachineSell> pages = new PageInfo<>(machineSell);
            return DataResult.ok(pages);
        } else if (accessType == 5) {
            //找工程
            List<ProjectSeek> projectSeek = projectSeekService.selectProjectSeekHome(type, types, brandId, modelId, title);
            //头像昵称
            for (ProjectSeek machineBuy1 : projectSeek) {
                QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", machineBuy1.getUserId());
                YxUser listy = yxUserMapper.selectOne(queryWrapper);
                machineBuy1.setNickname(listy.getNickname());
                machineBuy1.setAvatar(listy.getAvatar());
            }
            PageInfo<ProjectSeek> pages = new PageInfo<>(projectSeek);
            return DataResult.ok(pages);
        } else if (accessType == 6) {
            //物流信息
            List<TransportCompanyInfo> transportCompanyInfo = transportCompanyInfoService.selectTransportCompanyInfoHome(title, brandId, province, city, type, types, carTypeId);
            //头像昵称
            for (TransportCompanyInfo machineBuy1 : transportCompanyInfo) {
                QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", machineBuy1.getUserId());
                YxUser listy = yxUserMapper.selectOne(queryWrapper);
                machineBuy1.setNickname(listy.getNickname());
                machineBuy1.setAvatar(listy.getAvatar());
                QueryWrapper<CarTypes> queryWrappers = new QueryWrapper<>();
                queryWrappers.eq("id", machineBuy1.getCarTypeId());
                CarTypes carTypes = carTypesMapper.selectOne(queryWrappers);
                if (carTypes != null) {
                    machineBuy1.setCarTypeName(carTypes.getCarType());
                }
            }
            PageInfo<TransportCompanyInfo> pages = new PageInfo<>(transportCompanyInfo);
            return DataResult.ok(pages);
        } else {
            //托运信息
            List<TransportInfo> transportInfo = transportInfoService.selectTransportInfoHome(type, types, brandId, modelId, title, carTypeId);
            //头像昵称
            for (TransportInfo machineBuy1 : transportInfo) {
                QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", machineBuy1.getUserId());
                YxUser listy = yxUserMapper.selectOne(queryWrapper);
                machineBuy1.setNickname(listy.getNickname());
                machineBuy1.setAvatar(listy.getAvatar());
                QueryWrapper<CarTypes> queryWrappers = new QueryWrapper<>();
                queryWrappers.eq("id", machineBuy1.getCarTypeId());
                CarTypes carTypes = carTypesMapper.selectOne(queryWrappers);
                if (carTypes != null) {
                    machineBuy1.setCarTypeName(carTypes.getCarType());
                }

            }
            PageInfo<TransportInfo> pages = new PageInfo<>(transportInfo);
            return DataResult.ok(pages);
        }
    }


    /**
     * 查询资料库
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selectVipArticles")
    public DataResult selectVipArticles(Integer hot, Integer classifyId, String title,
                               Integer page, Integer limit, Integer userId,String brandId) {

        PageHelper.startPage(page, limit, true);
        List<VipArticle> list = vipArticleService.selectVipArticles(hot, classifyId, title,brandId);
        for (VipArticle vipArticle : list) {
            QueryWrapper<VipArticleCollect> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("article_id", vipArticle.getId()).eq("uid", userId);
            VipArticleCollect vipArticleCollect = vipArticleCollectMapper.selectOne(queryWrapper);
            if (vipArticleCollect != null) {
                vipArticle.setCollect(1);
            } else {
                vipArticle.setCollect(0);
            }
        }
        PageInfo<VipArticle> pages = new PageInfo<>(list);
        return DataResult.ok(pages);
    }

    /**
     * 根据id查询资料库信息
     *
     * @return
     */
    @RequestMapping("/selectVipArticlesById")
    public DataResult selectVipArticlesById(Integer id, Integer userId) {
        //资料库
        VipArticle vipArticle = vipArticleService.selectVipArticlesById(id);
        if (vipArticle == null) {
            return new DataResult(300, "资料已被删除", null);
        }
        String vipLv = vipArticle.getVipLv();
        List<VipUserInfo> uid = vipUserInfoMapper.selectList(new QueryWrapper<VipUserInfo>().eq("uid", userId));
        QueryWrapper<VipInfo> vipInfoQueryWrapper = new QueryWrapper<>();
        vipInfoQueryWrapper.eq("vip_id", uid.get(0).getVid());
        VipInfo vipInfo = vipInfoMapper.selectOne(vipInfoQueryWrapper);
        boolean b = uid.stream().allMatch(v -> v.getExpirationTime().compareTo(LocalDateTime.now()) < 0);
        if (vipLv.equals(vipInfo.getVipName()) && b) {
            return new DataResult(300, "需要会员", null);
        }
        QueryWrapper<VipArticleCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", id).eq("uid", userId);
        VipArticleCollect vipArticleCollect = vipArticleCollectMapper.selectOne(queryWrapper);
        if (vipArticleCollect != null) {
            vipArticle.setCollectId(vipArticleCollect.getId());
        }
        int count = vipArticleCollectService.selectVipArticleCollectCount(id, userId);
        vipArticle.setCollect(count > 0 ? 1 : 0);
        return DataResult.ok(vipArticle);
    }


    /**
     * 根据id查询资料库信息
     *
     * @return
     */
    @RequestMapping("/insertVipArticles")
    public DataResult insertVipArticles(VipArticleCollect vipArticleCollect) {
        vipArticleCollect.setCreateTime(LocalDateTime.now());
        int count = vipArticleCollectMapper.insert(vipArticleCollect);
        if (count > 0) {
            return DataResult.ok(1);
        }

        return DataResult.build(300, "失败");
    }


    /**
     * 添加意见反馈
     *
     * @return
     */
    @RequestMapping("/insertYxUserFeedBacks")
    public DataResult insertYxUserFeedBacks(YxUserFeedback yxUserFeedback) {
        yxUserFeedback.setCreateTime(LocalDateTime.now());
        int count = yxUserFeedbackMapper.insert(yxUserFeedback);
        if (count > 0) {
            return DataResult.ok(1);
        }

        return DataResult.build(300, "失败");
    }


    /**
     * 添加资讯收藏
     *
     * @return
     */
    @RequestMapping("/insertInfoCollect")
    public DataResult insertInfoCollect(InfoCollect infoCollect) {

        int count = infoCollectService.insertInfoCollect(infoCollect);
        if (count > 0) {
            return DataResult.ok(1);
        }

        return DataResult.build(300, "失败");
    }

    /**
     * 删除资讯收藏
     *
     * @return
     */
    @RequestMapping("/delInfoCollects")
    public DataResult delInfoCollects(InfoCollect infoCollect) {

        int count = infoCollectService.delInfoCollects(infoCollect);
        if (count > 0) {
            return DataResult.ok(1);
        }

        return DataResult.build(300, "失败");
    }



    /**
     * 查询我的收藏资讯1   司机招聘  2   司机找工作  3   找机械   4   二手机械出售     5   找工程 6   物流信息 7   托运信息 8去租赁
     *
     * @return
     */
    @RequestMapping("/selectInfoCollect")
    public DataResult selectInfoCollect(Integer userId) {

        HashMap<String, Object> map = new HashMap<>();
          //查询收藏表
        List<InfoCollect> list1 = infoCollectService.selectInfoCollect(userId, 1);
        for (InfoCollect infoCollect : list1) {
            //找司机
            List<DriverInfo> driverInfo = driverInfoService.selectDriverInfos(infoCollect.getInfoId());
            if (!CollectionUtils.isEmpty(driverInfo)) {
                infoCollect.setTitle(driverInfo.get(0).getTitle());//标题
                infoCollect.setImage(driverInfo.get(0).getImage());//图片
                infoCollect.setMessage(driverInfo.get(0).getMessage());//详情
            }
            map.put("list1", list1);
        }
           //查询收藏表
        List<InfoCollect> list2 = infoCollectService.selectInfoCollect(userId, 2);
            for (InfoCollect infoCollect2 : list2) {
            //找工作
            List<DriverWorkInfo> driverWorkInfo = driverWorkInfoService.selectDriverWorkInfos(infoCollect2.getInfoId());
            if (!CollectionUtils.isEmpty(driverWorkInfo)) {
                infoCollect2.setTitle(driverWorkInfo.get(0).getTitle());
                infoCollect2.setImage(driverWorkInfo.get(0).getImage());
                infoCollect2.setMessage(driverWorkInfo.get(0).getMessage());//详情
            }
                map.put("list2", list2);
            }
           //查询收藏表
        List<InfoCollect> list3 = infoCollectService.selectInfoCollect(userId, 3);
           for (InfoCollect infoCollect3 : list3) {
                   //找机械
                   List<MachineBuy> machineBuy = machineBuyService.selectMachineBuys(infoCollect3.getInfoId());
                   if (!CollectionUtils.isEmpty(machineBuy)) {
                       infoCollect3.setTitle(machineBuy.get(0).getTitle());
                       infoCollect3.setImage(machineBuy.get(0).getImage());
                       infoCollect3.setMessage(machineBuy.get(0).getMessage());//详情
                   }
            map.put("list3", list3);
        }
             //查询收藏表
        List<InfoCollect> list4 = infoCollectService.selectInfoCollect(userId, 4);
        for (InfoCollect infoCollect4 : list4) {
            //卖机械
            List<MachineSell> machineSell = machineSellService.selectMachineSells(infoCollect4.getInfoId());
            if (!CollectionUtils.isEmpty(machineSell)) {
                infoCollect4.setTitle(machineSell.get(0).getTitle());
                infoCollect4.setImage(machineSell.get(0).getImage());
                infoCollect4.setMessage(machineSell.get(0).getMessage());//详情
            }
            map.put("list4", list4);
        }
             //查询收藏表
        List<InfoCollect> list5 = infoCollectService.selectInfoCollect(userId, 5);
        for (InfoCollect infoCollect5 : list5) {
            //找工程
            List<ProjectSeek> projectSeek = projectSeekService.selectProjectSeeks(infoCollect5.getInfoId());
            if (!CollectionUtils.isEmpty(projectSeek)) {
                infoCollect5.setTitle(projectSeek.get(0).getTitle());
                infoCollect5.setImage(projectSeek.get(0).getImage());
                infoCollect5.setMessage(projectSeek.get(0).getMessage());//详情
            }
            map.put("list5", list5);
        }
            //查询收藏表
        List<InfoCollect> list6 = infoCollectService.selectInfoCollect(userId, 6);
        for (InfoCollect infoCollect6 : list6) {
            //物流信息
            List<TransportCompanyInfo> transportCompanyInfo = transportCompanyInfoService.selectTransportCompanyInfos(infoCollect6.getInfoId());
            if (!CollectionUtils.isEmpty(transportCompanyInfo)) {
                infoCollect6.setTitle(transportCompanyInfo.get(0).getTitle());
                infoCollect6.setImage(transportCompanyInfo.get(0).getImage());
                infoCollect6.setMessage(transportCompanyInfo.get(0).getMessage());//详情
            }
            map.put("list6", list6);
        }
            //查询收藏表
            List<InfoCollect> list7 = infoCollectService.selectInfoCollect(userId, 7);
        for (InfoCollect infoCollect7 : list7) {
            //托运信息
            List<TransportInfo> transportInfo = transportInfoService.selectTransportInfos(infoCollect7.getInfoId());
            if (!CollectionUtils.isEmpty(transportInfo)) {
                infoCollect7.setTitle(transportInfo.get(0).getTitle());
                infoCollect7.setImage(transportInfo.get(0).getImage());
                infoCollect7.setMessage(transportInfo.get(0).getMessage());//详情
            }
            map.put("list7", list7);
        }
        //查询收藏表
            List<InfoCollect> list8 = infoCollectService.selectInfoCollect(userId, 8);
        for (InfoCollect infoCollect8 : list8) {
            //去租赁
            List<Lease> lease = leaseService.selectLeases(infoCollect8.getInfoId());
            if (!CollectionUtils.isEmpty(lease)) {
                infoCollect8.setTitle(lease.get(0).getTitle());
                infoCollect8.setImage(lease.get(0).getLeaseImg());
                infoCollect8.setMessage(lease.get(0).getLeaseContext());//详情
            }
            map.put("list8", list8);
        }
            return DataResult.ok(map);
    }

    /**
     * 上传图片
     *
     * @return
     */
    @RequestMapping("/upload")
    public DataResult upload(@RequestParam("file") MultipartFile file, @RequestParam(defaultValue = "") String name) {
        return DataResult.ok(uploadFile.writePicture(file, name));
    }
}

